/* eslint-disable vue/no-v-html */
/* eslint-disable vue/no-v-html */
<template>
  <div class="login">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      label-position="left"
      label-width="0px"
      class="login-form"
    >
      <h3 class="title">{{ title }}</h3>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" placeholder="账号">
          <svg-icon
            slot="prefix"
            icon-class="user"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          placeholder="密码"
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
 
      <el-form-item prop="code">
        <el-input
          v-model="loginForm.code"
          placeholder="请输入验证码，不区分大小写"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="validCode"
            class="el-input__icon input-icon"
          />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" />
        </div>
      </el-form-item>
      <el-checkbox
        v-model="loginForm.rememberMe"
        style="margin:0px 0px 25px 0px;"
        >记住我</el-checkbox
      >
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div v-if="showFooter" id="el-login-footer">
      <!-- eslint-disable-next-line vue/no-v-html -->
      <span v-html="footerTxt" />
      <span>⋅</span>
      <a href="http://www.baidu.com/" target="_blank">{{ caseNumber }}</a>
    </div>

    <!-- 以下是选择登录社区的弹窗 -->
    <el-dialog
      title="请选择登录社区"
      :visible.sync="dialogChooseAss"
      width="600px"
    >
      <el-form ref="dataForm" :model="ass">
        <!-- @change="selectEquipmentTypeDialog(equipment.equipmentTypeIdDialog)" -->
        <el-select v-model="ass.equipmentTypeIdDialog">
          <el-option
            v-for="item in associatedCommunity"
            :key="item.communityAddress"
            :label="item.communityName"
            :value="item.communityAddress"
            :disabled="item.disabled"
          >
          </el-option>
        </el-select>
      </el-form>

      <div style="text-align: center" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogAddEquipmentSubmit()"
          >确 定</el-button
        >
        <el-button @click="dialogAddEquipment = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 以上是选择登录社区的弹窗 -->
  </div>
</template>

<script>
import waves from "@/directive/waves"; // Waves directive
import Settings from "@/settings";
import Cookies from "js-cookie";
import { encrypt } from "@/utils/rsaEncrypt";
import { getCodeImg } from "@/api/system/login";
import { userInfoDetail } from "@/api/system/user";
export default {
  name: "Login",
  directives: { waves },
  data() {
    return {
      title: Settings.title,
      showFooter: Settings.showFooter,
      footerTxt: Settings.footerTxt,
      caseNumber: Settings.caseNumber,
      codeUrl: "",
      cookiePass: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: ["blur", "change"],
            message: "用户名不能为空"
          }
        ],
        password: [
          {
            required: true,
            trigger: ["blur", "change"],
            message: "密码不能为空"
          }
        ],
        code: ""
        //[{ required: true, trigger: 'change', message: '验证码不能为空' }]
      },
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      dialogChooseAss: false,

      //以下参数是新加的参数
      associatedCommunity: [],
      ass: {},
      formLabelWidth: "120px"
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
    // token 过期提示
    this.point();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      let password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      // 保存cookie里面的加密后的密码
      this.cookiePass = password === undefined ? "" : password;
      password = password === undefined ? this.loginForm.password : password;
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        code: ""
      };
    },
    point() {
      const point = Cookies.get("point") !== undefined;
      if (point) {
        this.$notify({
          title: "提示",
          message: "当前登录状态已过期，请重新登录！",
          type: "warning",
          duration: 5000
        });
        Cookies.remove("point");
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        const user = {
          username: this.loginForm.username,
          password: this.loginForm.password,
          code: this.loginForm.code,
          uuid: this.loginForm.uuid
        };
        if (user.password !== this.cookiePass) {
          user.password = encrypt(user.password);
        }
        if (valid) {
          this.loading = true;
          if (user.rememberMe) {
            Cookies.set("username", user.username, {
              expires: Settings.passCookieExpires
            });
            Cookies.set("password", user.password, {
              expires: Settings.passCookieExpires
            });
            Cookies.set("rememberMe", user.rememberMe, {
              expires: Settings.passCookieExpires
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("system/user/login", user)
            .then(() => {
              this.loading = false;
              //获取用户信息,如果是admin直接登录不是就查找社区,
              //userInfoDetail();

              // userInfoDetail().then(response => {
              //   if (response == null) {
              //     //admin  直接进去
              //     this.$router.push({ path: "/" });
              //   } else {
              //     this.associatedCommunity = response;
              //     this.dialogChooseAss = true;
              //   }
              // });

              // 因为演示经常随便切换，权限，所以默认都跳转首页
              this.$router.push({ path: '/' })
              // this.$router.push({ path: this.redirect || '/' })
            })
            .catch(() => {
              this.loading = false;
              this.getCode();
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url(/static/images/bg-1.jpg);
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  display: inline-block;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
</style>
